//
//  startAnimationViewController.m
//  GameBoss
//
//  Created by 张艺 on 15/4/27.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "startAnimationViewController.h"

#define ScreenSize (375 / 8) * Screen.width / 375

@interface startAnimationViewController ()

// 开机动画的View
@property(nonatomic, retain) UIView *rootView;
@property(nonatomic, retain) UIImageView *imageViewG;
@property(nonatomic, retain) UIImageView *imageViewA;
@property(nonatomic, retain) UIImageView *imageViewM;
@property(nonatomic, retain) UIImageView *imageViewE;
@property(nonatomic, retain) UIImageView *imageViewB;
@property(nonatomic, retain) UIImageView *imageViewO;
@property(nonatomic, retain) UIImageView *imageViewS;
@property(nonatomic, retain) UIImageView *imageViewSS;
// 创建一个按钮来失去第一响应者
@property(nonatomic, retain) UIButton *returnButton;


@end

@implementation startAnimationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 开机动画的底图
    self.rootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen.width, Screen.height)];
    self.rootView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.rootView];
    
    [self.view bringSubviewToFront:self.rootView];
    [_rootView release];
    
    // 开机动画的G
    self.imageViewG =[self createImageView:CGRectMake(ScreenSize, 100 * Screen.height / 667 + ScreenSize * 3, ScreenSize, ScreenSize) image:[UIImage imageNamed:@"g"]];
    // 开机动画的A
    self.imageViewA = [self createImageView:CGRectMake(ScreenSize * 2, 100 * Screen.height / 667 + ScreenSize * 2, ScreenSize, ScreenSize) image:[UIImage imageNamed:@"a"]];
    // 开机动画的M
    self.imageViewM = [self createImageView:CGRectMake(3 * ScreenSize, 100 * Screen.height / 667 + ScreenSize, ScreenSize, ScreenSize) image:[UIImage imageNamed:@"m"]];
    // 开机动画的E
    self.imageViewE = [self createImageView:CGRectMake(4 * ScreenSize, 100 * Screen.height / 667, ScreenSize, ScreenSize) image:[UIImage imageNamed:@"e"]];
    // 开机动画的B
    self.imageViewB = [self createImageView:CGRectMake(- ScreenSize, Screen.height + ScreenSize, ScreenSize, ScreenSize) image:[UIImage imageNamed:@"b"]];
    // 开机动画的O
    self.imageViewO = [self createImageView:CGRectMake(9 * ScreenSize, [UIScreen mainScreen].bounds.size.height + ScreenSize, ScreenSize, ScreenSize) image:[UIImage imageNamed:@"o"]];
    // 开机动画的S
    self.imageViewS = [self createImageView:CGRectMake(9 * ScreenSize, 0, ScreenSize, ScreenSize) image:[UIImage imageNamed:@"s"]];
    // 开机动画的SS
    self.imageViewSS = [self createImageView:CGRectMake(9 * ScreenSize, 0, ScreenSize, ScreenSize) image:[UIImage imageNamed:@"s"]];

    
    // button
    self.returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.returnButton.frame = CGRectMake(0, Screen.height - 2 * Screen.width / 8, Screen.width, Screen.width / 8);
        self.returnButton.alpha = 0.0;
    [self.returnButton setTitle:@"进入应用" forState:UIControlStateNormal];
    [self.returnButton addTarget:self action:@selector(returnToMainView) forControlEvents:UIControlEventTouchUpInside];
    [self.rootView addSubview:self.returnButton];
    
    [self performSelector:@selector(showG) withObject:nil afterDelay:2.0];
    [self performSelector:@selector(showA) withObject:nil afterDelay:2.5];
    [self performSelector:@selector(showS) withObject:nil afterDelay:2.0f];
    [self performSelector:@selector(showSS) withObject:nil afterDelay:2.5f];
    [self performSelector:@selector(turnRoot) withObject:nil afterDelay:5.0f];
    [self performSelector:@selector(shutAnimation) withObject:nil afterDelay:0.0f];
    [self performSelector:@selector(buttonAppear) withObject:nil afterDelay:2.0f];
    
    
    
}

- (UIImageView *)createImageView:(CGRect)frame image:(UIImage *)image
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.image = image;
    [self.rootView addSubview:imageView];
    [imageView release];
    return imageView;
}


- (void)showG
{
    [UIView animateWithDuration:.5 delay:.5 usingSpringWithDamping:.01 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
        // G
        self.imageViewB.frame = CGRectMake(3 * ScreenSize, 250 * Screen.width / 375 + ScreenSize * 3, ScreenSize, ScreenSize);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)showA
{
    [UIView animateWithDuration:.5 delay:.5 usingSpringWithDamping:.5 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
        // A
        self.imageViewO.frame = CGRectMake(4 * ScreenSize, 250 * Screen.width / 375 + ScreenSize * 2, ScreenSize, ScreenSize);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)showS
{
    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:.5 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
        self.imageViewS.frame = CGRectMake(5 * ScreenSize, 250 * Screen.width / 375 + ScreenSize, ScreenSize, ScreenSize);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)showSS
{
    [UIView animateWithDuration:1 delay:0.1 usingSpringWithDamping:.05 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
        self.imageViewSS.frame = CGRectMake(6 * ScreenSize, 250 * Screen.width / 375, ScreenSize, ScreenSize);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)shutAnimation
{
    self.imageViewG.alpha = 0.0;
    self.imageViewA.alpha = 0.0;
    self.imageViewM.alpha = 0.0;
    self.imageViewE.alpha = 0.0;
    [UIView animateWithDuration:3.0f delay:0.0f options:UIViewAnimationOptionCurveLinear                     animations:^ {
        self.imageViewG.transform = CGAffineTransformMakeRotation((- 45 ) * M_PI / 180);
        self.imageViewA.transform = CGAffineTransformMakeRotation((- 45 ) * M_PI / 180);
        self.imageViewM.transform = CGAffineTransformMakeRotation((- 45 ) * M_PI / 180);
        self.imageViewE.transform = CGAffineTransformRotate(self.imageViewE.transform, - M_PI_4);
        
        self.imageViewG.alpha = 1.0;
        self.imageViewA.alpha = 1.0;
        self.imageViewM.alpha = 1.0;
        self.imageViewE.alpha = 1.0;
        
    } completion:^(BOOL finished) {                         // 完成后执行code
        
    }];
}

- (void)turnRoot
{
    [UIView animateWithDuration:1 delay:.5 usingSpringWithDamping:.5 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
        
    } completion:^(BOOL finished) {
//        [self.rootView removeFromSuperview];
//        self.window.rootViewController.view.hidden = NO;
//        self.block(NO);
        
    }];
}

- (void)buttonAppear
{
    self.returnButton.alpha = 0.0;
    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveLinear                     animations:^ {
        self.returnButton.frame = CGRectMake(0, Screen.height - 2 * Screen.width / 8, Screen.width, ScreenSize);
        self.returnButton.alpha = 1.0;
        } completion:^(BOOL finished) {
            
        }];
    
}

///////////// button实现跳转回 主页面
- (void)returnToMainView
{
    NSLog(@"点击成功");
    self.tabBarController.tabBar.hidden = NO;
    [self.view removeFromSuperview];
//    self.block(NO);
}


-(void)firstOpened{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);
    NSLog(@"%@", paths);
    NSString *DocumentDirectory = [paths objectAtIndex:0];
    
    [fileManager changeCurrentDirectoryPath:[DocumentDirectory stringByExpandingTildeInPath]];
    
    [fileManager removeItemAtPath:@"firstOpenFile" error:nil];
    
    NSString *path = [DocumentDirectory stringByAppendingPathComponent:@"firstOpenFile"];
    
    NSMutableData  *writer = [[NSMutableData alloc] init];
    
    [writer appendData:nil];
    
    [writer writeToFile:path atomically:YES];
    
    [writer release];
    
}

-(BOOL)isFirstOpen {                                                         //判断文件是否存在
    
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,  NSUserDomainMask,  YES) ;
    
    NSString *documentsDirectory =  [paths objectAtIndex:0] ;
    
    NSString *file = [documentsDirectory stringByAppendingPathComponent:@"firstOpenFile"] ;
    
    if ([[NSFileManager defaultManager]  fileExistsAtPath:file]) {
        
        return NO ;
        
    }
    
    return YES ;
    
}

- (void)dealloc
{
    [_rootView release];
    [_imageViewG release];
    [_imageViewA release];
    [_imageViewM release];
    [_imageViewE release];
    [_imageViewB release];
    [_imageViewO release];
    [_imageViewS release];
    [_imageViewSS release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
